﻿@model IEnumerable<ExportProfileModel>
@using SmartStore.Admin.Models.DataExchange;
@{
	var isSystemProfiles = (bool)ViewData["IsSystemProfile"];
	var titleResKey = isSystemProfiles ? "Admin.DataExchange.Export.SystemProfilesTitle" : "Admin.DataExchange.Export.UserProfilesTitle";
}

<div class="mb-5">
	<div class="admin-config-group">
		<div class="head@(!isSystemProfiles ? " border-0 mb-0" : "")">@T(titleResKey)</div>
	</div>

	@if (Model.Any())
	{
		if (isSystemProfiles)
		{
			<div class="alert alert-info alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				@Html.Raw(T("Admin.DataExchange.Export.SystemProfileNote"))
			</div>
		}

		<div class="table-responsive">
			<table class="table admin-table export-profile-list profile-list">
				<thead>
					<tr>
						<th class="text-center"></th>
						<th>@T("Admin.DataExchange.Export.Name")</th>
						<th>@T("Admin.DataExchange.Export.EntityType")</th>
						<th class="text-center">@T("Admin.DataExchange.Export.FileExtension")</th>
						<th class="text-center">@T("Admin.DataExchange.Export.ExportFiles")</th>
						<th>@T("Admin.System.ScheduleTasks.LastStart")</th>
						<th>@T("Admin.System.ScheduleTasks.NextRun")</th>
						<th></th>
					</tr>
				</thead>
				<tbody>
					@foreach (var profile in Model.OrderBy(x => x.Name))
					{
						<tr data-profile-id="@profile.Id" data-task-id="@profile.ScheduleTaskId" class="@(profile.Enabled ? "" : "disabled")">
							<td class="text-center">
								<img src="@profile.Provider.ThumbnailUrl" alt="@profile.Provider.FriendlyName" class="@(profile.Enabled ? "" : "o40")" />
							</td>
							<td>
								<a href="@Url.Action("Edit", new { id = profile.Id })"><strong>@profile.Name</strong></a>
								<div class="muted">@profile.ProviderSystemName</div>
							</td>
							<td>
								@profile.Provider.EntityTypeName.NaIfEmpty()
							</td>
							<td class="text-center">
								@if (profile.Provider.FileExtension.HasValue())
								{
									@Html.IconForFileExtension(profile.Provider.FileExtension, true)
								}
								else
								{
									<span class="muted">@("".NaIfEmpty())</span>
								}
							</td>
							<td id="ExportFileCount@(profile.Id)" class="text-center">
								@{ Html.RenderPartial("ProfileFileCount", profile.FileCount); }
							</td>
							<td>
								<div class="last-run-info">
									@{ Html.RenderPartial("~/Administration/Views/ScheduleTask/_LastRun.cshtml", profile.TaskModel); }
								</div>
							</td>
							<td class="progress-info">
								<div class="next-run-info">
									@{ Html.RenderPartial("~/Administration/Views/ScheduleTask/_NextRun.cshtml", profile.TaskModel); }
								</div>
								<div class="task-progress text-muted@(profile.IsTaskRunning ? "" : " hide")" data-task-id="@profile.ScheduleTaskId" data-running="@(profile.IsTaskRunning ? "true" : "false")"></div>
							</td>
							<td class="omega" style="width: 200px;">
								<button type="button" class="btn btn-block btn-sm btn-warning btn-run-task@(profile.IsTaskRunning || !profile.Enabled ? " hide" : "")"
										data-url="@Url.Action("Execute", "Export", new { area = "Admin", id = profile.Id })" data-task-id="@profile.ScheduleTaskId">
									<i class="fa fa-play"></i>
									<span>@T("Admin.System.ScheduleTasks.RunNow")</span>
								</button>

								<a href="@Url.Action("CancelJob", "ScheduleTask", new { id = profile.ScheduleTaskId })" class="btn btn-block btn-sm btn-secondary btn-cancel-task@(!profile.IsTaskRunning ? " hide" : "")"
								   data-task-id="@profile.ScheduleTaskId" onclick='return confirm(@T("Admin.Common.AreYouSure").JsText);'>
									<span>@T("Common.Cancel")</span>
								</a>

								<a href="@Url.Action("DownloadLogFile", new { id = profile.Id })" target="LogFile"
								   class="btn btn-block btn-sm btn-secondary btn-log@(profile.IsTaskRunning || !profile.Enabled || !profile.LogFileExists ? " d-none" : "")">
									<i class="far fa-file-alt"></i>
									<span>@T("Admin.Configuration.ActivityLog")</span>
								</a>
							</td>
						</tr>
				}
				</tbody>
			</table>
		</div>
	}
	else
	{
		<div class="alert alert-info alert-dismissible">
			@T("Admin.DataExchange.Export.NoProfiles")
			<button type="button" class="close" data-dismiss="alert">&times;</button>
		</div>
	}
</div>